using GalaSoft.MvvmLight;
using Microsoft.Practices.ServiceLocation;
using MuddyBoots.Greenlight.QC.Common.Patterns.Behaviour.Mediator;
using MuddyBoots.GreenlightQC.DataService;
using MuddyBoots.GreenlightQC.DataService.Model;
using MuddyBoots.GreenlightQC.Mobile.Persistence;
using MuddyBoots.GreenlightQC.Mobile.UI.Helpers;
using MuddyBoots.GreenlightQC.Mobile.UI.Model;
using NLog;
using System;

namespace MuddyBoots.GreenlightQC.Mobile.UI.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public partial class MainWindowViewModel : ViewModel
    {
        private readonly string instanceId = Guid.NewGuid().ToString();

        private readonly Logger log;

        private static readonly LoginViewModel loginViewModel = new LoginViewModel();

        private static readonly AssessmentHeaderViewModel assessmentHeaderViewModel = new AssessmentHeaderViewModel();

        private Menu menu;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainWindowViewModel(Logger log)
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}

            this.log = log;

            this.CurrentViewModel = MainWindowViewModel.loginViewModel;

            Mediator.Register("LoggedIn", this.instanceId, this.GetMenu);
        }

        private void GetMenu(object obj)
        {
            var service = ServiceLocator.Current.GetInstance<IGreenlightQCMobile>();
            var database = ServiceLocator.Current.GetInstance<IDatabaseEntities>();
            this.menu = service.GetMenu(database, 2);
            this.IsLoggedIn = true;
        }

        public override void Cleanup()
        {
            log.Debug("Cleanup...");
            Mediator.Unregister("LoggedIn", this.instanceId, this.GetMenu);
            base.Cleanup();
        }

        private void ShowAssessmentsExecute()
        {
            this.SubMenu = this.menu.Assessments;
        }

        private void ShowCorrectiveActionsExecute()
        {
            this.SubMenu = this.menu.CorrectiveActions;
        }

        private void ShowTasksExecute()
        {
            this.SubMenu = this.menu.Tasks;
        }

        private void ShowSynchroniseExecute()
        {
            this.SubMenu = this.menu.Synchronise;
        }

        private void ShowReportsExecute()
        {
            this.SubMenu = this.menu.Reports;
        }

        private void LoadAssessmentExecute()
        {
            this.CurrentViewModel = MainWindowViewModel.assessmentHeaderViewModel;
        }
    }
}